<template>
  <bjSelect
    v-bind="$attrs"
    v-on="$listeners"
    :placeholder="placeholder"
    :replaceFields="replaceFields"
    :dataResource="list"
    :loading="loading"
    :disabled="disabled"
    :value="value"
  ></bjSelect>
</template>

<script>
/**  查询标段  **/
import bjSelect from "@/components/bj-select";
import { queryEngineerName } from "@/api/wisdom-beam/cement_common";
import { getLoginData } from "@/tools";
export default {
  name: "SelectMixin",
  model: {
    prop: "value",
    event: "change",
  },
  components: {
    bjSelect,
  },
  props: {
    //标段id
    orgCode: {
      type: [Number, String],
      default: "",
    },
    //设备id
    equCode: {
      type: [Number, String],
      default: "",
    },
    placeholder: {
      type: String,
      default: "请选择工程名称",
    },
    value: {
      type: [Object, Array],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    manual: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    listenTarget() {
      let { orgCode, equCode, disabled } = this;
      return {
        orgCode,
        equCode,
        disabled,
      };
    },
  },
  watch: {
    listenTarget: {
      handler(val) {
        let { disabled } = val;
        if (!disabled) {
          !this.manual && this.render();
        } else {
          this.list = [];
          this.loading = false;
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      loading: true,
      replaceFields: { key: "projectName", label: "projectName" },
      list: [],
    };
  },
  mounted() {
    this.manual && this.render();
  },
  methods: {
    render() {
      setTimeout((_) => {
        this.getListData();
      });
    },
    //查询工程名称
    getListData() {
      let { userId, tenantCode, sysSource } = getLoginData();
      let { orgCode, equCode } = this;
      this.loading = true;
      queryEngineerName({
        tenantCode,
        userId,
        secCode: orgCode,
        equCode,
        sysSource,
      })
        .then((res) => {
          let { data } = res;
          this.list =
            data.result.map((v) => {
              return { projectName: v };
            }) || [];
          this.loading = false;
          this.$emit("response", res);
        })
        .catch((_) => {
          this.loading = false;
          this.list = [];
          this.$emit("response", null);
        });
    },
  },
};
</script>

<style></style>
